import * as vscode from 'vscode';
import { createContext } from '../utils/createContext';
import { intellisenseProvider } from "./intellisenseProvider";
import { tomlCompletionItem } from '../utils/tomlCompletionItem';

export const tomlProvider: intellisenseProvider = {
  selector: {
    scheme: "file",
    language: "toml"
  },
  provider: {
    provideCompletionItems,
  }
};

async function provideCompletionItems(document: vscode.TextDocument, position: vscode.Position): Promise<vscode.CompletionItem[]> {
  try {
    const context = createContext(document, position);
    const { textFullLine } = context;
    if (typeof textFullLine !== 'undefined' && textFullLine.length > 0) {
      const arr = tomlCompletionItem.create(textFullLine.trim(), context);
      return Promise.resolve(arr);
    }
  } catch { }
  return Promise.resolve([]);
}